package com.mk.controller.user;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mk.entity.Pet;
import com.mk.result.Result;
import com.mk.service.IPetService;
import com.mk.vo.PetNumberPlateVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 动物检疫合格证明业务
 */
@RestController
@RequestMapping("/user/animalCertificate")
@RequiredArgsConstructor
public class AnimalCertificateController {

    private final IPetService petService;

    /**
     * 动物检疫合格证明登记
     * @param animalCertificate
     * @param petId
     * @return
     */
    @CrossOrigin
    @PutMapping
    public Result registerAnimalCertificate(String animalCertificate, Long petId) {
        LambdaUpdateWrapper<Pet> wrapper = new LambdaUpdateWrapper<Pet>()
                .set(Pet::getAnimalCertificate, animalCertificate)
                .set(Pet::getAnimalStatus, Pet.NORMAL)
                .set(Pet::getAnimalTime, LocalDateTime.now())
                .set(Pet::getUpdateTime, LocalDateTime.now())
                .eq(Pet::getId, petId);
        petService.update(wrapper);
        return Result.success();
    }

    /**
     * 根据宠物id获取动物检疫合格证明信息
     * @param petId
     * @return
     */
    @CrossOrigin
    @GetMapping("/petId")
    public Result<PetNumberPlateVO> getAnimalCertificate(Long petId) {
        Pet pet = petService.getById(petId);
        PetNumberPlateVO petNumberPlateVO = new PetNumberPlateVO();
        petNumberPlateVO.setAnimalCertificate(pet.getAnimalCertificate());
        return Result.success(petNumberPlateVO);
    }

    /**
     * 根据id删除动物检疫合格证明信息
     * @param petId
     * @return
     */
    @CrossOrigin
    @DeleteMapping("/delete/{petId}")
    public Result deleteAnimalCertificate(@PathVariable Long petId) {
        petService.update(new LambdaUpdateWrapper<Pet>()
                .set(Pet::getAnimalCertificate, null)
                .set(Pet::getUpdateTime, LocalDateTime.now())
                .eq(Pet::getId, petId));
        return Result.success();
    }
}
